<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/28
 * Time: 9:11
 */
namespace app\web\service;

use app\web\model\ShopConfig;
use app\web\model\AddressInvoice;
use app\web\model\AddressExpress;
use think\facade\Log;

class BasicSettingService{
    /**
     * @todo 获取基础配置
     * @author ZJF
     */
    public function getConfig($shop_id = 0){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        try {
            //获取店铺快递发件人信息
            $express_list = $this->getAddressExpress($shop_id);
            //获取店铺发货单发件人信息
            $invoice_list = $this->getAddressInvoice($shop_id);
            //获取店铺配置信息
            $config_info = $this->getShopConfig($shop_id);
            $config_data = array(
                'express_list' => $express_list,
                'invoice_list' => $invoice_list,
                'config_info' => $config_info
            );
            return $config_data;
        }
        catch(\Exception $e){
            $error = array(
                'message'=>$e->getMessage(),
                'url'=>$e->getFile(),
                'code'=>$e->getCode(),
                'line'=>$e->getLine()
            );
            Log::write(json_encode($error,JSON_UNESCAPED_UNICODE),"ERROR");
            return array_response([],1,"异常错误");
        }
    }

    /**
     * @todo 设置基础配置
     * @author ZJF
     */
    public function editConfig($shop_id = 0,$param = []){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        if(empty($param)){
            return array_response([],1,"无效的基础配置");
        }
        $order_sort = empty($param['order_sort'])?0:$param['order_sort'];//订单排序
        $express_sort = empty($param['express_sort'])?0:$param['express_sort'];//快递单宝贝打印顺序
        $invoice_sort = empty($param['invoice_sort'])?0:$param['invoice_sort'];//发货单宝贝打印顺序
        $data = array(
            'shop_id'=>$shop_id,
            'mtime'=>date('Y-m-d H:i:s')
        );
        if(!empty($order_sort)){
            $data['order_sort'] = $order_sort;
        }
        if(!empty($express_sort)){
            $data['express_sort'] = $express_sort;
        }
        if(!empty($invoice_sort)){
            $data['invoice_sort'] = $invoice_sort;
        }
        $config_model = new ShopConfig();
        $res = $config_model->where("shop_id='{$shop_id}'")->save($data);
        if(false===$res){
            return array_response([],1,'设置失败');
        }
        return true;
    }

    /**
     * @todo 获取店铺快递发件人信息
     * @author ZJF
     */
    public function getAddressExpress($shop_id = 0){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        //获取店铺快递发件人信息
        $express_model = new AddressExpress();
        $express_list = $express_model
                        ->where("shop_id='{$shop_id}' AND is_del=0")
                        ->order("id DESC")
                        ->select();
        return ObjectToArray($express_list);
    }

    /**
     * @todo 获取店铺发货单发件人信息
     * @author ZJF
     */
    public function getAddressInvoice($shop_id = 0){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        $invoice_model = new AddressInvoice();
        $invoice_list = $invoice_model
                        ->where("shop_id='{$shop_id}' AND is_del=0")
                        ->order("id DESC")
                        ->select();
        return ObjectToArray($invoice_list);
    }

    /**
     * @todo 获取店铺配置
     * @author ZJF
     */
    public function getShopConfig($shop_id = 0){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        $config_model = new ShopConfig();
        $config_info = $config_model->where("shop_id='{$shop_id}'")->find();
        $config_info = ObjectToArray($config_info);
        if (empty($config_info)) {
            //无配置信息，新增
            $now_time = date('Y-m-d H:i:s');
            $config_data = array(
                'shop_id' => $shop_id,
                'order_sort' => 2,
                'express_sort' => 1,
                'invoice_sort' => 1,
                'filter' => 0,
                'ctime' => $now_time,
                'mtime' => $now_time
            );
            $res = $config_model->insert($config_data);
            $config_info = $config_data;
        }
        return $config_info;
    }

    /**
     * @todo 新增/编辑快递发件人信息
     * @author ZJF
     */
    public function editAddressExpress($shop_id = 0,$param = []){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        if(empty($param)){
            return array_response([],1,"无效的发件人信息");
        }
        $id = empty($param['id'])?0:$param['id'];
        $name = empty($param['name'])?'':$param['name'];
        $phone = empty($param['phone'])?'':$param['phone'];
        $sign_name = empty($param['sign_name'])?'':$param['sign_name'];
        $post_code = empty($param['post_code'])?'':$param['post_code'];
        $province = empty($param['province'])?'':$param['province'];
        $province_code = empty($param['province_code'])?0:$param['province_code'];
        $city = empty($param['city'])?'':$param['city'];
        $city_code = empty($param['city_code'])?0:$param['city_code'];
        $district = empty($param['district'])?'':$param['district'];
        $district_code = empty($param['district_code'])?0:$param['district_code'];
        $address = empty($param['address'])?'':$param['address'];
        $now_time = date('Y-m-d H:i:s');
        if(!is_numeric($id)){
            return array_response([],1,"无效的发件人ID");
        }
        if(empty($name)){
            return array_response([],1,'无效的发件人姓名');
        }
        if(empty($phone)){
            return array_response([],1,'无效的发件人电话');
        }
        if(empty($post_code) || !is_numeric($post_code)){
            return array_response([],1,'无效的邮编');
        }
        if(empty($province)){
            return array_response([],1,'无效的省');
        }
        if(empty($province_code) || !is_numeric($province_code)){
            return array_response([],1,'无效的省-行政编码');
        }
        if(empty($city)){
            return array_response([],1,'无效的市');
        }
        if(empty($city_code) || !is_numeric($city_code)){
            return array_response([],1,'无效的市-行政编码');
        }
        if(empty($district)){
            return array_response([],1,'无效的区');
        }
        if(empty($district_code) || !is_numeric($district_code)){
            return array_response([],1,'无效的区-行政编码');
        }
        if(empty($address)){
            return array_response([],1,'无效的详细地址');
        }
        try{
            $data = array(
                'shop_id'=>$shop_id,
                'name'=>$name,
                'phone'=>$phone,
                'sign_name'=>$sign_name,
                'post_code'=>$post_code,
                'province'=>$province,
                'province_code'=>$province_code,
                'city'=>$city,
                'city_code'=>$city_code,
                'district'=>$district,
                'district_code'=>$district_code,
                'address'=>$address,
                'mtime'=>$now_time
            );
            $express_model = new AddressExpress();
            if(empty($id)){
                //新增
                $data['ctime'] = $now_time;
                $res = $express_model->insert($data,true);
            }
            else{
                //修改
                $res = $express_model->where("id='{$id}'")->save($data);
            }
            if($res===false){
                return array_response([],1,empty($id)?'新增失败':'编辑失败');
            }
            return true;
        }
        catch(\Exception $e){
            $error = array(
                'message'=>$e->getMessage(),
                'url'=>$e->getFile(),
                'code'=>$e->getCode(),
                'line'=>$e->getLine()
            );
            Log::write(json_encode($error,JSON_UNESCAPED_UNICODE),"ERROR");
            return array_response([],1,"异常错误");
        }
    }

    /**
     * @todo 新增/编辑发货单发件人信息
     * @author ZJF
     */
    public function editAddressInvoice($shop_id = 0,$param = []){
        if(empty($shop_id) || !is_numeric($shop_id)){
            return array_response([],1,"无效的店铺ID");
        }
        if(empty($param)){
            return array_response([],1,"无效的发件人信息");
        }
        $id = empty($param['id'])?0:$param['id'];
        $title = empty($param['title'])?'':$param['title'];
        $phone = empty($param['phone'])?'':$param['phone'];
        $operator = empty($param['operator'])?'':$param['operator'];
        $shop_name = empty($param['shop_name'])?'':$param['shop_name'];
        $tips = empty($param['tips'])?'':$param['tips'];
        $now_time = date('Y-m-d H:i:s');
        if(!is_numeric($id)){
            return array_response([],1,"无效的发件人ID");
        }
        if(empty($title)){
            return json_ok([],1,'无效的发货标题');
        }
        if(empty($operator)){
            return json_ok([],1,'无效的经办人');
        }
        if(empty($shop_name)){
            return json_ok([],1,'无效的店铺名称');
        }
        if(empty($phone) || !checkPhone($phone)){
            return json_ok([],1,'无效的联系电话');
        }
        try{
            $data = array(
                'shop_id'=>$shop_id,
                'title'=>$title,
                'phone'=>$phone,
                'operator'=>$operator,
                'shop_name'=>$shop_name,
                'mtime'=>$now_time
            );
            if(!empty($tips)){
                $data['tips'] = $tips;
            }
            $invoice_model = new AddressInvoice();
            if(empty($id)){
                //新增
                $data['ctime'] = $now_time;
                $res = $invoice_model->insert($data,true);
            }
            else{
                //修改
                $res = $invoice_model->where("id='{$id}'")->save($data);
            }
            if($res===false){
                return array_response([],1,empty($id)?'新增失败':'编辑失败');
            }
            return true;
        }
        catch(\Exception $e){
            $error = array(
                'message'=>$e->getMessage(),
                'url'=>$e->getFile(),
                'code'=>$e->getCode(),
                'line'=>$e->getLine()
            );
            Log::write(json_encode($error,JSON_UNESCAPED_UNICODE),"ERROR");
            return array_response([],1,"异常错误");
        }
    }
}